/*
	VerbRemoveEnhancement.cpp

	This verb calls the use() mutator on an enhancement object
	(c)2008 Palestar, Jeff Everett
*/


#include "Debug/Assert.h"
#include "Debug/Profile.h"
#include "World/NounTarget.h"
#include "VerbRemoveEnhancement.h"
#include "Game/CargoEnhancement.h"
#include "GameContext.h"

//-------------------------------------------------------------------------------

IMPLEMENT_FACTORY( VerbRemoveEnhancement, Verb );

BEGIN_PROPERTY_LIST( VerbRemoveEnhancement, Verb )
	ADD_TRANSMIT_PROPERTY( m_pEnhancement );
END_PROPERTY_LIST();

VerbRemoveEnhancement::VerbRemoveEnhancement()
{}

VerbRemoveEnhancement::VerbRemoveEnhancement( Noun * a_pUser, Noun *a_pEnhancement)
	: m_pEnhancement(a_pEnhancement)
{
	if ( a_pEnhancement != NULL )
		attachVerb( a_pUser );
}

//----------------------------------------------------------------------------

Verb::Priority VerbRemoveEnhancement::priority() const
{
	return CRITICAL;
}

Verb::Scope VerbRemoveEnhancement::scope() const
{
	return SERVER;
}

bool VerbRemoveEnhancement::client() const
{
	return true;
}

bool VerbRemoveEnhancement::queue() const
{
	return false;
}

//----------------------------------------------------------------------------

bool VerbRemoveEnhancement::canAttach( Noun * pNoun )
{
	if (!validate(m_pEnhancement) && validate(pNoun))
		return false;
	return true;
}

void VerbRemoveEnhancement::onExecute()
{
	// if server apply as child
	NounShip * pShip = WidgetCast<NounShip>( target() );
	NounEnhancement *pTargetEnhancement = WidgetCast<NounEnhancement>(m_pEnhancement);
				pShip->InitializeTrail();

	if(pTargetEnhancement != NULL)
	{
		// go through the ship and remove the enhancement
		for(int i=0;i<pShip->childCount();i++)
		{
			NounEnhancement *pEnh = WidgetCast<NounEnhancement>(pShip->child(i));
			if(pEnh)
			{
				if(pEnh == pTargetEnhancement)
				{
					if(context()->server())
					{
						pEnh->setDetach();
						pShip->privateChat( CharString().format( "<color;ffffff>Enhancement: Removing <color;%6.6x>%s</color> from %s.",pEnh->color().RGB(), pEnh->GetName(), m_pTarget->name() ) );
						// save off storage with this "active" ship
						context()->user()->saveStorage(pShip);
					}
					pShip->InitializeTrail();
				}
			}
		}

		//Factory * pFactory = Factory::findFactory( pCargo->enhancementKey());
		//if ( pFactory != NULL )
		//{
		//	NounEnhancement *pEnh = NULL;
		//	// create the enhancement that we will add to the ship
		//	pEnh = WidgetCast<NounEnhancement>( pFactory->createWidget() );
		//	// attach the enhancement
		//	pShip->attachNode(pEnh);
		//	// detach the cargo
		//	pShip->detachNode(pCargo);

		//	pCargo->setSyncronized( false );
		//	pCargo->setDetach();
		//	// set message to player
			
		//}
	}
}

//----------------------------------------------------------------------------
// EOF
